<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/6/20
 * Time: 9:22
 */

namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类

class Budget extends Model
{
    protected $vat_price;
    protected $email;
    protected $subject;
    protected $body;


    //获取增值票金额
    public function vatPrice($tid){
        $query = new \yii\db\Query();

        //票据
        $vat = $query
            ->select(['key2'])
            ->from('ifssc_php.vat_ticket')
            ->where(['id' => $tid])
            ->one();

        if($vat['key2'] != null) {
            $ticket_vat_k2 = explode('≡', $vat['key2']);
            $this->vat_price=isset($ticket_vat_k2[12]) ? $ticket_vat_k2[12] : '0';

        }else{
            $this->vat_price = 0;
        }
        return $this->vat_price;
    }



    //修改增值票状态
    public function vatType($tid)
    {

        //票据
        $update_atlas_budget = Yii::$app->db->createCommand()->update('ifssc_php.vat_ticket', ['budget_type' => 3], ['id' => $tid])->execute();

        return $update_atlas_budget;
    }




    //修改科目余额
    public function updateBudgetPrice($fee_id,$vat_price,$price)
    {
        $query = new \yii\db\Query();

        $row = $query
            ->select(['id','fee_name','budget_total','buy_total'])
            ->from('ifssc_php.atlas_budget')
            ->where(['id' => $fee_id,'status' => 1])
            ->one();
        $a_price = (int)$row['buy_total'] + (int)$vat_price;//花费总计
        $budget_price = $row['budget_total'] + $price;
        if($a_price > $budget_price){
            return 3;
        }
        //修改数据库
        $time = date('Y-m-d H:i:s',time());
        $update_atlas_budget = Yii::$app->db->createCommand()->update('ifssc_php.atlas_budget', ['budget_total'=>$budget_price,'buy_total' => $a_price,'update_time' => $time], 'id = '.$fee_id)->execute();
        if($update_atlas_budget){
            return 1;
        }else{
            return 2;
        }

    }



    //减少科目余额
    public function lewBudgetPrice($lower_id,$price)
    {
        $query = new \yii\db\Query();

        $row = $query
            ->select(['id','fee_name','budget_total','buy_total'])
            ->from('ifssc_php.atlas_budget')
            ->where(['id' => $lower_id,'status' => 1])
            ->one();
        $budget_price = $row['budget_total'] - $price;
        //修改数据库
        $time = date('Y-m-d H:i:s',time());
        $update_atlas_budget = Yii::$app->db->createCommand()->update('ifssc_php.atlas_budget', ['budget_total'=>$budget_price,'update_time' => $time], 'id = '.$lower_id)->execute();
        if($update_atlas_budget){
            return 1;
        }else{
            return 2;
        }

    }


    //不能调增科目
    public function noAdd()
    {
        $query = new \yii\db\Query();
        $row = $query->select(['name'])->from('ifssc_php.budget_no_add')->all();
        return $row;

    }


    /**
     * title budget_type状态 名称等查询 one
     * @param $id
     */
    public function budgetType($id)
    {
        $query = new \yii\db\Query();
        //联表
        $atlas_budget = $query
            ->select('a.item_id,a.budget_total,a.buy_total,a.fee_name,a.fee_ids,b.id,b.budget_name,b.fee_type')
            ->from('ifssc_php.atlas_budget AS a')
            ->rightJoin('ifssc_php.budget_type AS b','b.id = a.fee_ids')
            ->where(['a.id'=>$id])
            ->one();
        return $atlas_budget;
    }

    /**
     * title 添加 科目调整表
     * @param $price
     * @param $lower_id
     * @param $add_id
     * @param $create_time
     * @param $status
     * @return int
     * @throws \yii\db\Exception
     */
    public function budgetAdd($price,$lower_id,$add_id,$create_time,$status)
    {
        $table = 'budget_adjust';
        $field = ['price','few_id','add_id','create_time','status'];
        $batchData = [[$price,$lower_id,$add_id,$create_time,$status]];
        $add = Yii::$app->db->createCommand()->batchInsert($table,$field,$batchData)->execute();
        return $add;
    }



}